let utils = require('../../../utils/util');
const db = wx.cloud.database({      env: "treeworld-2g5yvz9s899bf4a6"})
const app = getApp()
let globalData = getApp().globalData
const key = globalData.key
Page({
  data: {
    isIPhoneX: globalData.isIPhoneX,
    message: 'Css，前端最华丽的外衣，让用户打开新体验的大门，比如此页面',
    cityDatas: {},
    hourlyDatas: [],
    weatherIconUrl: globalData.weatherIconUrl,
    hasPopped: false,
    // 是否切换了城市
    located: true,
    // 需要查询的城市
    bcgImgIndex: 0,
    bcgImg: '',
    bcgImgAreaShow: false,
    showHeartbeat: true,
    // heartbeat 时禁止搜索，防止动画执行
    enableSearch: true,
    openSettingButtonShow: false,
  },


  list(){
    db.collection('press').where({
      type:"Css"
    }).limit(10).get().then(res => {
      console.log(res)
      this.setData({
        xw_list: res.data
      })
    })
  },
  tz(e) {
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: "../../pages/" + e.currentTarget.dataset.a + "?id=" + e.currentTarget.dataset.id
    })
  },
  getLocation: function () {
    var that = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res);
        var latitude = res.latitude||28.13551;
        var longitude = res.longitude||113.03555;
        that.getWeather(`${latitude},${longitude}`)
      },
      fail: function () {
        wx.showToast({
          title: '小程序获取坐标失败，请手动输入地址查询或设置允许使用地理位置',
          icon: 'none',
        })
        that.getCityDatas();
      }
    })
  },
  // 获取缓存信息
  getCityDatas() {
    let cityDatas = wx.getStorage({
      key: 'cityDatas',
      success: (res) => {
        this.setData({
          cityDatas: res.data,
        })
      },
    })
  },
  //获取天气信息
  getWeather(location) {
    wx.request({
      url: `${globalData.requestUrl.weather}`,
      data: {
        location,
        key,
      },
      success: (res) => {
        console.log(res)
        if (res.statusCode === 200) {
          let data = res.data.HeWeather6[0];
          console.log(data);
          if (data.status === 'ok') {
            this.success(data, location)
          } 
        }
      },
      fail: () => {
        wx.showToast({
          title: '查询失败',
          icon: 'none',
        })
      },
    })
  },
  success(data, location) {
    this.setData({
      openSettingButtonShow: false,
      searchCity: location,
    })
    wx.stopPullDownRefresh()
    let now = new Date()
    // 存下来源数据
    data.updateTime = now.getTime()
    data.updateTimeFormat = utils.formatDate(now, "MM-dd hh:mm")
    wx.setStorage({
      key: 'cityDatas',
      data,
    })
    this.setData({
      cityDatas: data,
    })
  },
 

  getCity: function (val) {
    var that = this;
    var BMap = new bmap.BMapWX({
      ak: 'L2gPxbsFywGaCQIIhPKDYzFgcsEPS3I5'
    });
    BMap.geocoding({
      address: val,
      success: function (res) {
        let latitude = res.result.location.lat;
        let longitude = res.result.location.lng;
        that.getWeather(`${latitude},${longitude}`);
      },
      fail: function () {
        wx.showToast({
          title: '查询失败',
          icon: 'none',
        })
      },
    });
  },
  onPullDownRefresh() {
    this.reloadPage()
  },

  setNavigationBarColor(color) {
    let bcgColor = color || this.data.bcgColor
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: this.data.bcgColor,
    })
  },

onLoad() {
    this.list()
    this.getLocation();
    var date = new Date();
    var h = date.getHours();
    let message = "";
    console.log(h)
 

  },

})
